package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkKnowledgeApproval;
import com.ysd.lis.entity.WkKnowledgeCatalog;
import com.ysd.lis.entity.WkKnowledgeContent;
import com.ysd.lis.entity.WkKnowledgeIssue;
import com.ysd.lis.mapper.WkKnowledgeApprovalMapper;
import com.ysd.lis.mapper.WkKnowledgeContentMapper;
import com.ysd.lis.mapper.WkKnowledgeIssueMapper;
import com.ysd.lis.request.KnowledgePageParam;
import com.ysd.lis.service.WkKnowledgeContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-03-04
 */
@Service
@Slf4j
public class WkKnowledgeContentServiceImpl extends ServiceImpl<WkKnowledgeContentMapper, WkKnowledgeContent> implements WkKnowledgeContentService {

    @Autowired
    WkKnowledgeContentMapper wkKnowledgeContentMapper;
    @Autowired
    WkKnowledgeIssueMapper wkKnowledgeIssueMapper;
    @Autowired
    WkKnowledgeApprovalMapper wkKnowledgeApprovalMapper;

    @Override
    public Result getContentListByCatalogId(String catalogId) {
        MPJLambdaWrapper<WkKnowledgeContent> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .distinct()
                .selectAll(WkKnowledgeContent.class)
                .selectCollection(WkKnowledgeIssue.class, WkKnowledgeContent::getWkKnowledgeIssueList)
                .leftJoin(WkKnowledgeIssue.class, WkKnowledgeIssue::getContentId, WkKnowledgeContent::getId)
                .eq(WkKnowledgeContent::getDelFlag, 0)
                .eq(WkKnowledgeContent::getCatalogId, catalogId)
                .orderByAsc(WkKnowledgeContent::getSeq);
        List<WkKnowledgeContent> wkKnowledgeContents = wkKnowledgeContentMapper.selectJoinList(WkKnowledgeContent.class, mpjLambdaWrapper);
        return Result.succ(1, "查询成功！", wkKnowledgeContents);
    }

    /**
     * 根据id获取内容信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getContentListById(String id) {
        MPJLambdaWrapper<WkKnowledgeContent> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper
                .distinct()
                .selectAll(WkKnowledgeContent.class)
                .selectCollection(WkKnowledgeIssue.class, WkKnowledgeContent::getWkKnowledgeIssueList)
                .leftJoin(WkKnowledgeIssue.class, WkKnowledgeIssue::getContentId, WkKnowledgeContent::getId)
                .eq(WkKnowledgeContent::getDelFlag, 0)
                .eq(WkKnowledgeContent::getId, id)
                .orderByAsc(WkKnowledgeContent::getSeq);
        WkKnowledgeContent wkKnowledgeContent = wkKnowledgeContentMapper.selectJoinOne(WkKnowledgeContent.class, lambdaWrapper);
        return Result.succ(1, "查询成功！", wkKnowledgeContent);
    }

    @Override
    public Result addKnowledgeContent(WkKnowledgeContent content) {
        int insert = wkKnowledgeContentMapper.insert(content);
        if (insert > 0) {
            return Result.succ(1, "添加成功", content);
        } else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editKnowledgeContent(WkKnowledgeContent content) {
        int updateById = wkKnowledgeContentMapper.updateById(content);
        if (updateById > 0) {
            return Result.succ(1, "修改成功", content);
        } else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result submitKnowledgeContent(WkKnowledgeContent content) {
        int updateById = wkKnowledgeContentMapper.updateById(content);
        if (updateById > 0) {
            //生成审批记录表
            WkKnowledgeApproval wkKnowledgeApproval = new WkKnowledgeApproval();
            wkKnowledgeApproval.setContentId(content.getId());
            wkKnowledgeApproval.setSubmitter(content.getSubmitter());
            wkKnowledgeApproval.setSubmitReason("");
            wkKnowledgeApproval.setSubmitTime(content.getEditTime());
            wkKnowledgeApproval.setOrgId(content.getOrgId());
            wkKnowledgeApproval.setStatus(0);
            wkKnowledgeApproval.setClassify(content.getClassify());
            wkKnowledgeApproval.setTempContent(content.getTempContent());

            wkKnowledgeApprovalMapper.insert(wkKnowledgeApproval);
            return Result.succ(1, "修改成功", content);
        } else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletKnowledgeContent(WkKnowledgeContent content) {
        //查询内容下是否有问题列表
        LambdaQueryWrapper<WkKnowledgeIssue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WkKnowledgeIssue::getDelFlag,0)
                .eq(WkKnowledgeIssue::getContentId,content.getId());
        long cnt = wkKnowledgeIssueMapper.selectCount(queryWrapper);
        if(cnt>0){
            return Result.fail("节点下有问题列表，不能删除");

        }else{
            int deleteById = wkKnowledgeContentMapper.deleteById(content.getId());
            if (deleteById > 0) {
                return Result.succ(1, "删除成功", content);
            } else {
                return Result.fail("删除失败");
            }
        }
    }

    @Override
    public Result batchUpdateKnowledgeContent(WkKnowledgeCatalog catalog) {
        List<WkKnowledgeContent> wkKnowledgeContentList = catalog.getWkKnowledgeContentList();
        boolean b = this.updateBatchById(wkKnowledgeContentList);

        if (b) {
            return Result.succ(1, "更新成功",null);
        } else {
            return Result.fail("更新失败");
        }
    }

    @Override
    public Result getContentByContentId(KnowledgePageParam param) {

        MPJLambdaWrapper<WkKnowledgeContent> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .distinct()
                .selectAll(WkKnowledgeContent.class)
                .selectCollection(WkKnowledgeIssue.class, WkKnowledgeContent::getWkKnowledgeIssueList)
                .leftJoin(WkKnowledgeIssue.class, WkKnowledgeIssue::getContentId, WkKnowledgeContent::getId)
                .eq(WkKnowledgeContent::getDelFlag, 0)
                .eq(WkKnowledgeContent::getId, param.getContentId())
                .orderByAsc(WkKnowledgeContent::getSeq);
        WkKnowledgeContent wkKnowledgeContent = wkKnowledgeContentMapper.selectJoinOne(WkKnowledgeContent.class, mpjLambdaWrapper);
        return Result.succ(1, "查询成功！", wkKnowledgeContent);
    }
}
